import {ModalForm, ProFormText} from "@ant-design/pro-components";
import {GenerateTableVO} from "@/services/GenerateTableService";
import {useRef, useState} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";
import {Tabs, TabsProps} from "antd";
import GenerateColumnTable from "@/pages/Generate/components/GenerateColumnTable";
import {GenerateColumnVO} from "@/services/GenerateColumnService";

export interface GenerateTableUpdateModalProps {
  onColumnsChange: (columns: GenerateColumnVO[]) => void;
  onFinish: (formData: any) => Promise<boolean | void>;
  generateTable?: GenerateTableVO;
  open: boolean;
  onOpenChange: (state: boolean) => void;
  columns?: GenerateColumnVO[];
}

const GenerateTableUpdateModal = (props: GenerateTableUpdateModalProps) => {
  const formRef = useRef<ProFormInstance>()
  const [inst, setInst] = useState<GenerateTableVO | undefined>()

  const onOpenChange = (state: boolean) => {
    if (state) {
      setInst(props.generateTable || undefined)
      formRef?.current?.setFieldsValue(props.generateTable)
    } else {
      setInst(undefined)
      formRef?.current?.resetFields()
    }
    props.onOpenChange(state);
  }

  const TableInfo = () => <>
    <ProFormText label="id" hidden name="id"></ProFormText>
    <ProFormText label="业务注释" name="businessComment"></ProFormText>
    <ProFormText label="业务名" name="businessName"></ProFormText>
    <ProFormText label="表名" name="tableName"></ProFormText>
    <ProFormText label="业务注释" name="packageName"></ProFormText>
  </>;


  const items: TabsProps["items"] = [
    {
      key: '1',
      label: `业务信息`,
      children: <TableInfo/>,
    },
    {
      key: '2',
      label: `字段信息  `,
      children: <GenerateColumnTable businessId={props.generateTable?.id} columns={props.columns}
                                     onColumnsChange={props.onColumnsChange}/>,
    },
  ]

  return <>

    <ModalForm
      width='1500px'
      formRef={formRef}
      title={props.generateTable ? "修改代码生成表" : "添加代码生成表"}
      open={props.open}
      initialValues={inst}
      onFinish={props.onFinish}
      onOpenChange={onOpenChange}
    >
      <Tabs defaultActiveKey="1" items={items}>
      </Tabs>
    </ModalForm>
  </>
}

export default GenerateTableUpdateModal
